import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Dialog弹窗',
      home: DialogPage(),
    );
  }
}

class DialogPage extends StatefulWidget {
  const DialogPage({super.key});

  @override
  State<DialogPage> createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> {
  void _alertDialog() async {
    var result = await showDialog(
        barrierDismissible: true,
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("提示信息"),
            content: const Text("是否要删除"),
            actions: [
              TextButton(
                  onPressed: () {
                    print("ok");
                    Navigator.of(context).pop("确定-");
                    // Navigator.pop(context);
                  },
                  child: const Text("确定")),
              TextButton(
                  onPressed: () {
                    print("cancel");
                    Navigator.of(context).pop("取消-");
                  },
                  child: const Text("取消")),
            ],
          );
        });

    print("-alertDialog-${result}");
  }

  void _simpleDialog() async {
    var result = await showDialog(
        barrierDismissible: true, //点击背景是否消失弹出框
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: const Text("请选择语言"),
            children: [
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  // Navigator.of(context).pop();
                  Navigator.pop(context, "中文");
                },
                child: const Text("中文"),
              ),
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: const Text("英文"),
              ),
              const Divider(),
              SimpleDialogOption(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: const Text("日文"),
              ),
            ],
          );
        });

    print("-simpleDialog-${result}");
  }

  void _modelBottomSheet() async {
    double height = 220;

    var result = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Stack(
            children: <Widget>[
              Container(
                height: height,
                width: double.infinity,
                color: Colors.black54,
              ),
              Container(
                height: height,
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(25),
                      topRight: Radius.circular(25),
                    )),
              ),
              Container(
                height: height,
                child: Column(
                  children: <Widget>[
                    ListTile(
                      title: const Text("分享A"),
                      onTap: () {
                        Navigator.pop(context, "分享A");
                      },
                    ),
                    const Divider(),
                    ListTile(
                      title: const Text("分享B"),
                      onTap: () {
                        Navigator.pop(context, "分享B");
                      },
                    ),
                    const Divider(),
                    ListTile(
                      title: const Text("分享C"),
                      onTap: () {
                        Navigator.pop(context, "分享C");
                      },
                    ),
                  ],
                ),
              ),
            ],
          );
        });

    print("-modelBottomSheet-${result}");
  }

  void _toast() {
    print("_toast");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Dialog弹窗'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
                onPressed: _alertDialog,
                child: const Text("alert弹出框-alertDialog")),
            ElevatedButton(
                onPressed: _simpleDialog,
                child: const Text("simple弹出框-simpleDialog")),
            ElevatedButton(
                onPressed: _modelBottomSheet,
                child: const Text("model弹出框-modelBottomSheet")),
          ],
        ),
      ),
    );
  }
}
